﻿namespace 测反应时间
{
    internal class Program
    {
        enum Direction
        {
            up,
            down,
            left,
            right
        }
        const int countoftest = 10;

        static void Main(string[] args)
        {
            //定义变量 反应时间，计错次数
            double reactiontime = 0;
            int countoferror = 0;
            //生成随机数
            Random rnd = new Random();
            for (int i = 0; i < countoftest; i++)
            {
                //获取起始时间
                DateTime timestart = DateTime.Now;
                Console.Clear();
                Console.WriteLine($"现在正在第{i}次测试，总耗时{reactiontime}秒，共错误{countoferror}次");
                Direction direction = (Direction)rnd.Next(4);
                Console.WriteLine(direction.ToString());
                //var key = Console.ReadKey();
                bool flag = false;
                do
                {
                    var key = Console.ReadKey();

                    if (direction == Direction.up)
                    {
                        if (key.Key == ConsoleKey.UpArrow)
                            flag = true;
                        else countoferror++;
                        break;
                    }
                    else if (direction == Direction.down)
                    {
                        if (key.Key == ConsoleKey.DownArrow)
                            flag = true;
                         else countoferror++;
                        break;
                    }
                    else if (direction == Direction.left)
                    {
                        if (key.Key == ConsoleKey.LeftArrow)
                            flag = true;
                        else countoferror++;
                        break;
                    }
                    else if (direction == Direction.right)
                    {
                        if (key.Key == ConsoleKey.RightArrow)
                            flag = true;
                        else countoferror++;
                        break;
                    }

                } while (flag);
                DateTime timeend = DateTime.Now;
                reactiontime = (timeend - timestart).TotalSeconds;

            }
        }
    }
}